package gui.nodes;

import gui.AbstractViewPane;
import gui.MainController;
import gui.MatiereUIConsulter;
import gui.SEEUIConsulter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;

import utility.Record;
import business.MatiereBL;

public class MatiereNode extends AbstractNode {

	int matId;
	String matName;
	int ueId;
	DefaultMutableTreeNode MatiereNode;
	UENode parentNode;

	public MatiereNode(UENode parentNode, Record nomMatiere, int ueId) {
		this.parentNode = parentNode;
		this.matId = nomMatiere.getId();
		this.matName = nomMatiere.getLibelle();
		this.ueId = ueId;

	}

	@Override
	public JPopupMenu makePopup() {
		return new Popup();
	}

	@Override
	public String getNodeName() {
		return matName;
	}

	public void loadPrimaryViewPane() {
		AbstractViewPane newViewPane = new MatiereUIConsulter(parentNode,
AbstractViewPane.ViewMode.CONSULTATION, matId,ueId);
		MainController.getInstance().setViewPane(newViewPane);
		}	
	
	public void loadModifierViewPane() {
		AbstractViewPane newViewPane = new MatiereUIConsulter(parentNode,
AbstractViewPane.ViewMode.MODIFICATION, matId,ueId);
		MainController.getInstance().setViewPane(newViewPane);
		}
	public void loadSupprimerViewPane() {
		if (supprimer() == JOptionPane.YES_OPTION) {
			MatiereBL matiereBL = new MatiereBL(matId);
			matiereBL.supprimerMatiere();
		}
		((AbstractNode)parent).refresh();
		}
	
	public void loadAjouterViewPane() {
		AbstractViewPane newViewPane = new SEEUIConsulter(parentNode,
AbstractViewPane.ViewMode.AJOUT, -1, matId);
		MainController.getInstance().setViewPane(newViewPane);
		}

	private class Popup extends JPopupMenu implements ActionListener {
		JMenuItem ajouter = new JMenuItem("Ajouter un SEE");
		JMenuItem modifier = new JMenuItem("Modifier la matière");
		JMenuItem supprimer = new JMenuItem("Supprimer la matière");

		public Popup() {
			ajouter.addActionListener(this);
			modifier.addActionListener(this);
			supprimer.addActionListener(this);
			add(ajouter);
			add(modifier);
			add(supprimer);
		}

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == ajouter) {
				loadAjouterViewPane();
			}
			if (e.getSource() == modifier) {
				loadModifierViewPane();
			}
			if (e.getSource() == supprimer) {
				loadSupprimerViewPane();
			}
		}
	}

	@Override
	public void expandHook() {
		this.removeAllChildren();
		
		MatiereBL matBL = new MatiereBL(matId);
		
		System.out.println(matBL.getDescriptionMatiere());

		for (Record nomSEE : matBL.getListeSEE()) {
			SEENode seeNode = new SEENode(this, nomSEE,matId);
			add(seeNode);
		}
	}
	
	public int supprimer() {
		return JOptionPane.showConfirmDialog(
			    null,
			    "Êtes vous sûr de vouloir supprimer la matière\n"+
			    //ensBL.getName()+" "+ensBL.getPrenom()+
			    " ?",
			    "Avertissement",
			    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
		
	}

}
